<?php

if (!defined('IN_XLP')) {
    exit('Access Denied');
}

/**
 * Description of serviceAction
 * 默认首页
 * @author xlp
 */
class serviceAction extends commonAction {

    protected $menuFile = 'weixin/wxmenu'; //缓存菜单名文件

    function __construct() {
        parent::__construct();
        parent::_checkLogin();
        $this->sex = array('未知', '男', '女');
        $this->status = array(0 => '<span class="red">无效</span>', 1 => '正常', 2 => '<span class="grey">冻结</span>', 3 => '<span class="red">取消关注</span>');
        $this->assign(array('status' => $this->status, 'regType' => array(0 => '主动关注', 1 => '二维码扫描', 2 => '网站二维码', 3 => '地推'), 'auth' => array('否', '<span class="red">是</span>')));
    }

    /*
     * 后台默认首页 
     */

    function index() {
        loadAppFile('face.fun');
        $p = $this->_getid('p');
        $uid = $this->_getid('uid', 0);
        $status = $this->_getid('status', 0);
        $ftime = $this->_getid('ftime', 10);
        $lastId = $this->_getid('last_id', 0);
        $isAjax = $this->_getid('is_ajax', 0);
        $st = $this->_get('st', 'content');
        $title = $this->_get('q');
        $wxMenuData = F($this->menuFile);
        $where = array('aid' => admin::$adminInfo['aid']);
        $newData = false;
        if ($title && $st == 'user') {
            $user = D('member')->field('uid')->where(array('LIKE' => array('nickname' => $title)))->findAll();
//            z($user);
            if ($user) {
                $uid = array();
                foreach ($user as $v) {
                    $uid[] = $v['uid'];
                }
                $title = '';
            }
        } elseif ($title && $st == 'uid') {
            $uid = $title;
            $title = '';
        }
        if ($uid) {
            $where['uid'] = $uid;
        }
        if ($status) {
            $where['msg_type'] = $status;
        }
        if ($title) {
            $where['LIKE'] = array('content' => parent::safeSearch($title));
        }
        if ($lastId) {
            $where['id[>]'] = $lastId;
            $rs = M('access_log')->field('id,uid,msg_id,msg_type,content,create_time')->where($where)->limit(20)->order('id desc')->findAll();
        } else {
            $rs = M('access_log')->field('id,uid,msg_id,msg_type,content,create_time')->where($where)->page($p)->order('id desc')->findAll();
        }
//        z(M('access_log')->getAllSql());
        if ($rs) {
            $lastId = 0;
            foreach ($rs as $k => $v) {
                if (!$lastId) {
                    $lastId = $v['id'];
                }
                if ($v['msg_type'] == 1) {
                    $keyName = md5(str_replace('&amp;', '&', $v['content']));
                    $rs[$k]['content'] = isset($wxMenuData[$keyName]) ? $wxMenuData[$keyName] : $v['content'];
                } elseif ($v['msg_type'] == 2) {
                    $rs[$k]['content'] = getFaceContent($v['content']);
                } elseif ($v['msg_type'] == 4) {
                    $rs[$k]['content'] = '<a href="' . getImgUrl($v['content']) . '" rel="pop">' . $v['content'] . '</a>';
                }
            }
            $newData = true;
        }
        $this->assign(array('rs' => $rs, 'p' => $p, 'total' => M('access_log')->getTotal(), 'lastId' => $lastId, 'ftime' => $ftime, 'status' => $status, 'q' => $title, 'st' => $st, 'uid' => $uid));
        if ($isAjax) {
            $this->JsonReturn('ok', array('lastId' => $lastId, 'count' => count($rs), 'item' => $this->getFetch('log_item'), 'newData' => $newData), 1);
        } else {
            $this->display();
        }
    }

    function msg() {
        $uid = $this->_getid('uid');
        if (!$uid) {
            $this->returnJson = false;
            $this->JsonReturn('参数丢失');
        }
        $checkTime = TIME - (24 * 60 * 60 * 2);
        //检查用户是否48小时内有回复
        $checkHas = M('access_log')->field('id')->where(array('uid' => $uid, 'create_time[>=]' => $checkTime, 'create_time[<=]' => TIME))->find();
        if (!$checkHas) {
            $this->returnJson = false;
            $this->JsonReturn('用户48小时内没有互动，无法发送消息');
        }
        $this->assign(array('uid' => $uid, 'face' => C('face')));
        $this->display();
    }

    function send() {
        $uid = $this->_post('uid');
        $content = parent::_postContent('content');
        if (!$uid) {
            $this->JsonReturn('参数丢失', array('code' => '', 'msg' => ''), 0);
        }
        if (!$content) {
            $this->JsonReturn('回复内容不能为空', array('code' => '', 'msg' => ''), 0);
        }
        $touser = D('member')->getOpenidByUids($uid);
        if (!$touser) {
            $this->JsonReturn('用户不存在', array('code' => '', 'msg' => ''), 0);
        }
        $sendInfo = array(
            'touser' => $touser,
            'msgtype' => 'text',
            'text' => array(
                'content' => $content
            )
        );
//        z($sendInfo);
        T('weixin/weixin.api');
        $weixinMsgApi = new weixinMsgApi();
        if ($weixinMsgApi->sendCustomMessage($sendInfo)) {
            return $this->JsonReturn('回复成功', null, 1);
        } else {
            return $this->JsonReturn('回复失败', array('code' => $weixinMsgApi->errCode, 'msg' => $weixinMsgApi->errMsg), 0);
        }
    }

    function member() {
        $p = $this->_getid('p', 1);
        $q = $this->_get('q', '', 'wordsFilter');
        $status = $this->_getid('status', 0);
        $village_id = $this->_getid('village_id', 0);
        $source_id = $this->_getid('source', 0);
        $inviteid = $this->_getid('invite', 0);
        $st = $this->_get('st', 'name');
        $uid = $this->_getid('uid', 0);
        $where = ' 1=1 ';
        if ($q) {
            if ($st == 'name') {
                $where.=" AND (username LIKE '%" . parent::safeSearch($q) . "%' OR nickname LIKE '%" . parent::safeSearch($q) . "%')";
            } elseif ($st == 'phone') {
                $where.=" AND phone LIKE '%" . parent::safeSearch($q) . "%'";
            } elseif ($st == 'uid') {
                $where.=" AND uid = " . intval($q);
            }
        }
        if ($village_id) {
            $where .=' AND village_id=' . $village_id;
        }
        if ($status) {
            $where .=' AND status=' . ($status - 1);
        }
        if ($source_id) {
            $where .=' AND source_id=' . ($source_id - 1);
        }
        if ($inviteid) {
            $where .=' AND invite_uid=' . $inviteid;
        }
        if ($uid) {
            $where .=' AND uid=' . $uid;
        }
        $pageShow = 20;
        $limit = ($p - 1) * $pageShow . ',' . $pageShow;
        $rs = D('member')->query("SELECT a.*,b.title AS village_name FROM __TABLE__ AS a LEFT JOIN __PRE__village AS b ON a.village_id=b.vid WHERE " . $where . " ORDER BY a.uid DESC LIMIT " . $limit);
        $this->assign(array('rs' => $rs, 'p' => $p, 'q' => $q, 'total' => D('member')->where($where)->count()));
        $this->display();
    }

    function _empty() {
        return $this->index();
    }

}

//根据类型值获取菜单类型
function getMsgType($type = '') {
    switch ($type) {
        //事件推送
        case 1;
        case 'event';
            $objData = '菜单';
            break;
        //用户上行文本信息
        case 2;
        case 'text';
            $objData = '文本';
            break;
        //用户上行位置信息
        case 3;
        case 'location';
            $objData = '位置';
            break;
        //用户上行图片消息
        case 4;
        case 'image';
            $objData = '图片';
            break;
        //用户发送语音信息
        case 5;
        case 'voice';
            $objData = '语音';
            break;
        //用户上行连接信息
        case 6;
        case 'link';
            $objData = '链接';
            break;
        //用户上行视频信息
        case 7;
        case 'video';
            $objData = '视频';
            break;
        //用户关注
        case 8;
        case 'subscribe';
            $objData = '用户关注';
            break;
        //用户上行视频信息
        case 9;
        case 'unsubscribe';
            $objData = '取消关注';
            break;
        //用户上行视频信息
        case 10;
        case 'scan';
            $objData = '扫描二维码';
            break;
        default:
            //用户发送未知信息时回复
            $objData = '未知';
    }
    return $objData;
}
